﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Datorgrafik_lab2.Helpers {
    public struct rectangular{
        public float x{get;set;}
        public float y{get;set;}
    }
    public struct polar{
        public float r{get;set;}
        public float a{get;set;}
    }
    public struct cartesian {
        public float x{get;set;}
        public float y{get;set;}
        public float z{get;set;}
    }
    public struct spherical {
        public float r{get;set;}
        public float a{get;set;}
        public float b{get;set;}        
    }

    public class PositionConverter {
        private float SQUARED(float f) {
            return f * f;
        }

        public polar toPolar(rectangular point) {
            return new polar {
                a = (float)Math.Sqrt(SQUARED(point.x) + SQUARED(point.y)),
                r = (float)Math.Atan(point.y / point.x)
            };
        }

        public rectangular toRectangular(polar point) {
            return new rectangular {
                x = (float)(point.r * Math.Cos(point.a)),
                y = (float)(point.r * Math.Sin(point.a))
            };
        }

        public spherical toSpherical(cartesian point) {
            return new spherical {
                a = (float)Math.Sqrt(SQUARED(point.x) + SQUARED(point.y) + SQUARED(point.z)),
                b = (float)Math.Atan(point.y / point.x),
                r = (float)Math.Atan(Math.Sqrt(SQUARED(point.x) + SQUARED(point.y)) / point.z)
            };
        }

        public cartesian toCartesian(spherical point) {
            return new cartesian {
                x = (float)(point.r * Math.Cos(point.a) * Math.Sin(point.b)),
                y = (float)(point.r * Math.Sin(point.a) * Math.Sin(point.b)),
                z = (float)(point.r * Math.Cos(point.b))
            };
        }
    }
}